package com.cloud.workTest;

import com.warrenstrange.googleauth.GoogleAuthenticator;
import com.warrenstrange.googleauth.GoogleAuthenticatorKey;
import lombok.SneakyThrows;

public class TotpGeneratorTest {

    public static GoogleAuthenticatorKey generateTotpKey() {
        GoogleAuthenticator gAuth = new GoogleAuthenticator();
        return gAuth.createCredentials();
    }

    public static int generateTOTP(String secretKey) {
        GoogleAuthenticator gAuth = new GoogleAuthenticator();
        return gAuth.getTotpPassword(secretKey, System.currentTimeMillis());
    }

    public static void main(String[] args) throws InterruptedException {
        GoogleAuthenticatorKey googleAuthenticatorKey = TotpGeneratorTest.generateTotpKey();
        String secretKey = googleAuthenticatorKey.getKey();
        System.out.println(secretKey);

        Thread thread1 = new Thread(new Task("IHXVG4C47GVOSKFT"));
        Thread thread2 = new Thread(new Task("IHXVG4C47GVOSKFT"));

        thread1.start();
        thread2.start();
    }

    public static class Task implements Runnable {
        String secretKey;

        public Task (String secretKey) {
            this.secretKey = secretKey;
        }

        @SneakyThrows
        @Override
        public void run() {
            while (true) {
                Thread.sleep(3000);
                System.out.println(TotpGeneratorTest.generateTOTP(secretKey));
            }
        }
    }
}
